<?
define('APPBUGTRACKER','1.00');
/************************************************************************/
/* FlatNuke - Flat Text Based Content Management System                 */
/* ============================================                         */
/*                                                                      */
/* Copyright (c) 2003-2004 by Simone Vellei                             */
/* http://flatnuke.sourceforge.net                                      */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

// Section Bug Trackers
// with flatnkuke

//--------------------------------------------------------------------------
// formato file bug
//--------------------------------------------------------------------------
// 0 priority 		VALORE 0 10
// 1 submitted 		$MYFORUM
// 2 title 		DESCRIZIONE BREVISSIMA
// 3 description	DESCRIZIONR DETAGLIATA
// 4 suggestion		SUGGERIMENTI DEL SUBMITTER
// 5 example		FRAMMENTO DI CODICE PER IL TEST DEL BACO
// 6 fixedreport 	REPORT DI CORREZIONE
//--------------------------------------------------------------------------


if (eregi("application.inc",$_SERVER['PHP_SELF']))
	{ Header("Location: ../../index.php");
	die();
}

// VARIABILI RICEVIBILI
$modname =$_GET['mod'];
$action=$_GET['action'];
$filename=$_GET['filename'];
$dirname=$_GET['dirname'];


// include"config.php";
//fissa $siteurl se non finisce con /
if (substr($siteurl,-1)!="/")$siteurl=$siteurl."/";


//carica la lingua se esiste
if (file_exists("applications/$appname/.lang/$lang.php"))
    include "applications/$appname/.lang/$lang.php";
else
    include "applications/$appname/.lang/it.php";


// le directory di lavoro
$bugsdir="datas/applications/bug_tracker/bugs";
$fixdir="datas/applications/bug_tracker/fixed";
$filedir="datas/applications/bug_tracker/files";
// genera le cartelle bug_tracker se non ci sono in misc
if (!file_exists("datas/applications/bug_tracker"))mkdir("datas/applications/bug_tracker",0755);
if (!file_exists("datas/applications/bug_tracker/bugs"))mkdir("datas/applications/bug_tracker/bugs",0755);
if (!file_exists("datas/applications/bug_tracker/fixed"))mkdir("datas/applications/bug_tracker/fixed",0755);
if (!file_exists("datas/applications/bug_tracker/files"))mkdir("datas/applications/bug_tracker/files",0755);


if ($action=="")$action="bugs";


// genera il menu
echo "
    <table>
    <tr>
    <td>	[<b><a href='index.php?appname=$appname&action=report'>"._REPORT_A_BUG."	</a></b>]</td>
    <td>	[<b><a href='index.php?appname=$appname&action=bugs'>"._LIST_BUGS."		</a></b>]</td>
    <td>	[<b><a href='index.php?appname=$appname&action=fixed'>"._LIST_FIXED."	</a></b>]</td>
    <td>	[<b><a href='index.php?appname=$appname&action=help'>"._HELP."		</a></b>]</td>
    </tr>
    </table>
    ";
//---------------------------------------------------------------------------------------------------------------------------------------

// action report: il visualizzatore / editore dei bug sottomessi
if ($action=="report"){

    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/Announce.png' border=0></td>\n";
    echo "<td><h1>"._BUG_REPORT."</h1></td>\n";
    echo "</tr></table>\n";

    // se e' un file nuovo si attribuisce il nome
    if ($filename==""){
	$filename=time();
	$dirname=$bugsdir;
    }

    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin())
	 echo"<h2><FONT color='#cc0000' size='+1'>"._WARNING_USER."</h2>\n";

    // se esiste si caricano i valori
    if (file_exists("$dirname/$filename")){
	$f=file("$dirname/$filename");
	$priority=$f[0];
	$submitted=$f[1];
	$title=$f[2];
	$description=urldecode(substr($f[3],0,-1));
	$suggestion=urldecode(substr($f[4],0,-1));
	$example=$f[5];
	$fixedreport=urldecode(substr($f[6],0,-1));
if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()) {
//	if ((getlevel($_COOKIE['myforum'],"home")!=10) or versecid($_COOKIE['myforum'])){
	    if (trim($submitted) != trim($_COOKIE['myforum'])){
		echo"<h2>"._WARNING_POST."</h2>\n";
	    }
	}
    }
    else{
    	$priority=0;
	$submitted=$_COOKIE['myforum'];
	$title="";
	$description="";
	$suggestion="";
	$example="";
	$fixedreport="";
    }



    echo "
    <hr>\n
    <form name='report' enctype='multipart/form-data' action='index.php?appname=$appname&action=exec_report' method='post' >\n
    <table width='100%'>\n

    <input type='hidden' name='filename' value='$filename' />\n
    <input type='hidden' name='dirname' value='$dirname' />\n
    ";
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
 //   if((getlevel($_COOKIE['myforum'],"home")==10) and versecid($_COOKIE['myforum'])){
	echo "
	<tr bgcolor='#ffeeee'>\n
	<td><b>"._PRIORITY."</b></td>
	<td><select name='priority' value='$priority'>\n";
	echo"<option>$priority</option>\n";
	for ($i=0;$i<=5;$i++)echo"<option>$i</option>\n";
	echo"</select>\n <b>"._SUBMITTED_BY."</b>    <input type=text name='submitted' value='$submitted' /></td>\n
	</tr>\n
	";
    }else{
	echo "
	<input type=hidden name='priority' value='$priority' />\n
	<input type=hidden name='submitted' value='$submitted' />\n
	";
    }
    echo "
    <tr bgcolor='#ffffee'>\n
    <td><b>"._BUG_TITLE."</b></td><td><input type=text name='title' value='$title' size='33' /></td>\n
    </tr>\n
    <tr bgcolor='#ffeeff'>\n
    <td><b>"._DESCRIPTION."</b></td><td><textarea name='description' rows='5' cols='50' >$description</textarea></td>\n
    </tr>\n
    <tr bgcolor='#eeeeff'>\n
    <td><b>"._FIX_SUGGESTIONS."</b></td><td><textarea name='suggestions' rows='5' cols='50' >$suggestion</textarea></td>\n
    </tr >\n
    <tr>\n
    <tr bgcolor='#ffeeee'>\n
    <td><b>"._EXAMPLE."</b></td><td><input type=file name='example' size='42' />";
    if ($example !="")
	echo "<b> submited: </b> <a href=\"$filedir/$example\">$example</a>
	<input type=hidden name='oldexample' value='$example' />\n
	";

    echo "
    </td>\n
    </tr>\n
    ";
if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin() || $dirname==$fixdir){
//    if(((getlevel($_COOKIE['myforum'],"home")==10) and versecid($_COOKIE['myforum']))|| $dirname==$fixdir){
	echo "
	<tr bgcolor='#ddffdd'>\n
	<td><b>"._FIXED_REPORT."</b></td><td><textarea name='fixedreport' rows='5' cols='50' >$fixedreport</textarea></td>\n
	</tr>\n
	";
    }

    echo "
    <tr bgcolor='#ffaaaa'>\n
    <td></td><td><input type='submit' /> <input type='reset' /> </td>\n
    </tr>\n
    </table>\n
    <hr />
    ";

    //amministrazione
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
//    if((getlevel($_COOKIE['myforum'],"home")==10) and versecid($_COOKIE['myforum'])){
    echo "
    <table>
    <tr>
    <td><img src='applications/$appname/.images/Wip.png' border=0></td>\n
    <td><h2><FONT size='-1'> "._BUGTRACKER_ADMIN." </h2></td>
    <td>[<b><a href='index.php?appname=$appname&action=delete&dirname=$dirname&filename=$filename'><FONT size='-1'>"._DELETE."</a></b>]</td> \n
    <td>[<b><a href='index.php?appname=$appname&action=fix&dirname=$dirname&filename=$filename'><FONT size='-1'>"._FIX."</a></b>]</td> \n
    <td>[<b><a href='index.php?appname=$appname&action=unfix&dirname=$dirname&filename=$filename'><FONT size='-1'>"._UNFIX."</a></b>]  </td> \n
    </tr>
    </table>
    ";
    }

    echo "<hr/>\n";

}
//---------------------------------------------------------------------------------------------------------------------------------------


if ($action=="exec_report"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/Announce.png' border=0></td>\n";
    echo "<td><h1>"._REPORT."</h1></td>\n";
    echo "</tr></table>\n";
   if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
 //  if(getlevel($_COOKIE['myforum'],"home")>=0 ){

	$myforum=((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin());
	$priority=$_POST['priority'];
	$filename=$_POST['filename'];
	$dirname=$_POST['dirname'];
	$title=$_POST['title'];
	$description=urlencode($_POST['description']);
	$suggestion=urlencode($_POST['suggestions']);
	$oldexample=$_POST['oldexample'];
	$example=$filename."-".$_FILES['example']['name'];
	$fixedreport=urlencode($_POST['fixedreport']);

    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
//	if(getlevel($_COOKIE['myforum'],"home")==10 ){
	    $submitted=$_POST['submitted'];
	}else{
	    $submitted=$_COOKIE['myforum'];
	}

	if (!file_exists("$dirname/$filename") || defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser()) || admin_isadmin()) {
        if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){

		if ( move_uploaded_file($_FILES['example']['tmp_name'],"$filedir/".$filename."-".$_FILES['example']['name'])) {
		    if (strtolower(substr($_FILES['example']['name'],-4))==".php" ){
			unlink("$filedir/".$filename."-".$_FILES['example']['name']);
		    }
		    else{
			chmod( "$filedir/".$filename."-".$_FILES['example']['name'],0775);
			print "<hr>"._SUBMITTED_VALID."";
			print("<br/>$bugsdir =\".bugsdir\"<br/>\n");
			print_r($_FILES);
		    }
		} else {
		    print _WARINING_INVALID."<br>\n";
		    print_r($_FILES);
		}
		print "<br/>"._BUG_TITLE."=$title<br/>\n";
		print "<br/>"._DESCRIPTION."=$description<br/>\n";
		print "<br/>"._FIX_SUGGESTIONS."=$suggestion<br/>\n";
		print _SUBMITTED_BY." $submitted <br/>\n";

		if ($_FILES['example']['name']=="")
		    $example=$oldexample;

		if ($oldexample!="" && $oldexample!=$example)unlink("$filedir/".$oldexample);
		$hf=fopen("$dirname/$filename","w");
		fwrite($hf,"$priority\n");
		fwrite($hf,"$submitted\n");
		fwrite($hf,"$title\n");
		fwrite($hf,"$description\n");
		fwrite($hf,"$suggestion\n");
		fwrite($hf,"$example\n");
		fwrite($hf,"$fixedreport\n");
		fclose($hf);

		echo "<b>"._TANK_YOU."</b><br/>\n";

		echo "<p align='right'>[<a href='index.php?appname=$appname&action=bugs'>"._CONTINUE."</a>]</p><br/><br/><br/><hr />";
	    }

	    else
		die(_AUTH_ERR);

	}
    }
    else
	die(_NONPUOI);
}
//---------------------------------------------------------------------------------------------------------------------------------------

// action report: il visualizzatore / editore dei bug sottomessi
if ($action=="suggestion"){

    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/Fix.png' border=0></td>\n";
    echo "<td><h1>"._SUBMIT_SUGGESTION."</h1></td>\n";
    echo "</tr></table>\n";

    // se esiste si caricano i valori
    if (file_exists("$dirname/$filename")){
	$f=file("$dirname/$filename");
	$priority=$f[0];
	$submitted=$f[1];
	$title=$f[2];
	$description=urldecode(substr($f[3],0,-1));
	$suggestion=urldecode(substr($f[4],0,-1));
	$example=$f[5];
	$fixedreport=urldecode(substr($f[6],0,-1));
    }

    echo "
    <hr>\n
    <form name='suggestion' enctype='multipart/form-data' action='index.php?appname=$appname&action=exec_suggestion' method='post' >\n
    <table>\n

    <input type='hidden' name='filename' value='$filename' />\n
    <input type='hidden' name='dirname' value='$dirname' />\n
    ";

    echo "
    <tr>\n
    <td>"._FIX_SUGGESTIONS."</td><td><textarea name='suggestions' rows='5' cols='60' ></textarea></td>\n
    </tr>\n
    ";

    echo "
    <tr>\n
    <td></td><td><input type='submit' /> <input type='reset' /> </td>\n
    </tr>\n
    </table>\n
    <hr />
    ";

}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="exec_suggestion"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/Fix.png' border=0></td>\n";
    echo "<td><h1>"._SUGGESTION."</h1></td>\n";
    echo "</tr></table>\n";
   if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
//   if(getlevel($_COOKIE['myforum'],"home")>=0 ){

	$filename=$_POST['filename'];
	$dirname=$_POST['dirname'];
	$my_suggestion=$_POST['suggestions'];


	if (file_exists("$dirname/$filename") ){
	    $f=file("$dirname/$filename");
	    $priority=$f[0];
	    $submitted=$f[1];
	    $title=$f[2];
	    $description=urldecode(substr($f[3],0,-1));
	    $suggestion=urldecode(substr($f[4],0,-1));
	    $example=$f[5];
	    $fixedreport=urldecode(substr($f[6],0,-1));

	    $hf=fopen("$dirname/$filename","w");
	    fwrite($hf,"$priority");
	    fwrite($hf,"$submitted");
	    fwrite($hf,"$title");
	    fwrite($hf,urlencode("$description")."\n");
	    fwrite($hf,urlencode("$suggestion\n"." [".$_COOKIE['myforum']."]:$my_suggestion")."\n");
	    fwrite($hf,"$example");
	    fwrite($hf,urlencode("$fixedreport")."\n");
	    fclose($hf);

	    echo "<b>"._TANK_YOU."</b><br/>\n";
	    echo "<p align='right'>[<a href='index.php?appname=$appname&action=bugs'>"._CONTINUE."</a>]</p><br/><br/><br/><hr />";
	}
	else
	    die(_WARINING_INVALID);
    }
    else
	die(_NONPUOI);

}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="bugs"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/BugFound.png' border=0></td>\n";
    echo "<td><h1>"._LIST_BUGS."</h1></td>\n";
    echo "</tr></table>\n";

    $filter=$_GET['filter'];

    echo "<b>"._PRIORITY_FILTER."</b> . <select name='filter' onchange=\"javascript:window.location.href='index.php?appname=$appname&action=$action&filter='+value; \" >\n";
    echo"<option>$filter</option>\n";
    echo"<option></option>\n";
    for ($i=0;$i<=5;$i++)echo"<option>$i</option>\n";
    echo"</select>\n <br/>\n";

    echo "<hr/><table border=1>\n";
    echo "
    <tr bgcolor='ff0000' >\n
    <td ><h2><FONT size='-1'> ID </h2></td><td><h2><FONT size='-1'> "._PRIORITY." </h2></td><td><h2><FONT size='-1'> "._BUG_TITLE." </h2></td><td><h2><FONT size='-1'> "._SUBMITTED_BY." </h2></td><td><h2><FONT size='-1'> "._VIEW." </h2></td><td><h2><FONT size='-1'> "._SUGGESTIONS." </h2></td>\n
    </tr>\n
    ";

    $hdir=opendir($bugsdir);
    $i=0;
    while (false !== ($fa[$i++]= readdir($hdir)));
    closedir($hdir);
    for($i=0;$i<6;$i++){
	if ($filter != "" )$i=0+$filter;
	foreach($fa as $f ){
	    if (substr($f,0,1)!="." && $f!=""){
		$fl=file("$bugsdir/$f");
		if ($i == $fl[0]){
		    $view="<a href='index.php?appname=$appname&action=report&dirname=$bugsdir&filename=$f'>"._VIEW."</a>";
		    $suggestion="<a href='index.php?appname=$appname&action=suggestion&dirname=$bugsdir&filename=$f'>"._SUGGESTION."</a>";

		    if($fl[0]==0){echo "<tr bgcolor='#e0e0e0'>\n";$fl[0]=_UNASSIGNED;}
		    if($fl[0]==1)echo "<tr bgcolor='#F06000'>\n";
		    if($fl[0]==2)echo "<tr bgcolor='#F08000'>\n";
		    if($fl[0]==3)echo "<tr bgcolor='#F0A000'>\n";
		    if($fl[0]==4)echo "<tr bgcolor='#F0C000'>\n";
		    if($fl[0]==5)echo "<tr bgcolor='#F0F000'>\n";

		    echo "
		    <td><b> $f </b></td><td><b> ".$fl[0]." </b></td><td><b> ".$fl[2]." </b></td><td><b> ".$fl[1]." </b></td><td><b> $view </b></td><td><b> $suggestion </b></td>\n
		    </tr>\n
		    ";
		}
	    }
	}
	if ($i." " == $filter." ")break;
    }
    echo "</table >\n";

    echo "<hr/>\n";
}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="fixed"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/BugFix.png' border=0></td>\n";
    echo "<td><h1>"._LIST_FIXED."</h1></td>\n";
    echo "</tr></table>\n";

    $filter=$_GET['filter'];

    echo "<b>"._PRIORITY_FILTER."</b> . <select name='filter' onchange=\"javascript:window.location.href='index.php?appname=$appname&action=$action&filter='+value; \" >\n";
    echo"<option>$filter</option>\n";
    echo"<option></option>\n";
    for ($i=0;$i<=5;$i++)echo"<option>$i</option>\n";
    echo"</select>\n <br/>\n";

    echo "<hr/><table border=1>\n";
    echo "
    <tr bgcolor=\"00ff00\">\n
    <td><h2><FONT size='-1'> ID </h2></td><td><h2><FONT size='-1'> "._PRIORITY." </h2></td><td><h2><FONT size='-1'> "._TITLE." </h2></td><td><h2><FONT size='-1'> "._SUBMITTED_BY." </h2></td><td><h2><FONT size='-1'> "._VIEW." </h2></td>\n
    </font>
    </tr>\n
    ";
    $hdir=opendir($fixdir);
    $i=0;
    while (false !== ($fa[$i++]= readdir($hdir)));
    closedir($hdir);
    for($i=0;$i<6;$i++){
	if ($filter != "" )$i=0+$filter;
	foreach($fa as $f ){
	    if (substr($f,0,1)!="." && $f!=""){
		$fl=file("$fixdir/$f");
		if ($i==$fl[0]){
		    $view="<a href='index.php?appname=$appname&action=report&dirname=$fixdir&filename=$f'>"._VIEW."</a>";

		    if($fl[0]==0){echo "<tr bgcolor='#e0e0e0'>\n";$fl[0]=_UNASSIGNED;}
		    if($fl[0]==1)echo "<tr bgcolor='#F06000'>\n";
		    if($fl[0]==2)echo "<tr bgcolor='#F08000'>\n";
		    if($fl[0]==3)echo "<tr bgcolor='#F0A000'>\n";
		    if($fl[0]==4)echo "<tr bgcolor='#F0C000'>\n";
		    if($fl[0]==5)echo "<tr bgcolor='#F0F000'>\n";

		    echo "
		    <td><b> $f </b></td><td><b> ".$fl[0]." </b></td><td><b> ".$fl[2]." </b></td><td><b> ".$fl[1]." </b></td><td><b> $view </b></td>\n
		    </tr>\n
		    ";
		}
	    }
	}
	if ($i." " == $filter." ")break;
    }
    echo "</table >\n";

    echo "<hr/>\n";
}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="delete"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/Out.gif' border=0></td>\n";
    echo "<td><h1>"._REPORT_DELETED."</h1></td>";
    echo "</tr></table><hr>\n";
    $f=file("$dirname/$filename");
    $example=trim($f[5]);
    unlink("$filedir/$example");
    unlink("$dirname/$filename");
    echo "<p align='right'>[<a href='index.php?appname=$appname&action=bugs'>"._CONTINUE."</a>]</p><br/><br/><br/><hr />";
}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="fix"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/BugFix.png' border=0></td>\n";
    echo "<td><h1>"._BUG_FIXED."</h1></td>\n";
    echo "</tr></table><hr>\n";
    copy("$dirname/$filename","$fixdir/$filename");
    if ($dirname!=$fixdir)unlink("$dirname/$filename");
    echo "<p align='right'>[<a href='index.php?appname=$appname&action=bugs'>"._CONTINUE."</a>]</p><br/><br/><br/><hr />";

}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="unfix"){
    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/BugFound.png' border=0></td>\n";
    echo "<td><h1>"._BUG_RESUBMITTED."</h1></td>";
    echo "</tr></table><hr>\n";

    copy("$dirname/$filename","$bugsdir/$filename");
    if ($dirname==$fixdir)unlink("$dirname/$filename");
    echo "<p align='right'>[<a href='index.php?appname=$appname&action=bugs'>"._CONTINUE."</a>]</p><br/><br/><br/><hr />";

}
//---------------------------------------------------------------------------------------------------------------------------------------

if ($action=="help"){

    echo "<table><tr>\n";
    echo "<td><img src='applications/$appname/.images/Help.png' border=0></td>\n";
    echo "<td><h2>"._HELP_TITLE."</h2></td>";
    echo "</tr></table>\n";

    echo "<br/>"._HELP_DOC."<br />";
    echo "<p align='right'>[<a href='index.php?appname=$appname&action=bugs'>"._CONTINUE."</a>]</p><br/><br/><br/><hr />";
}
//---------------------------------------------------------------------------------------------------------------------------------------

?>

